package com.fh.practice.service.impl;/**
 * 描述:
 * Author:袁亚强
 * Date:2022/1/2413:52
 */

import com.fh.practice.entity.Area;
import com.fh.practice.entity.User;
import com.fh.practice.entity.UserVo;
import com.fh.practice.mapper.UserMapper;
import com.fh.practice.service.UserService;
import com.fh.practice.utils.EmailThread;
import com.fh.practice.utils.yyqMD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * created by macro on 2022/1/24,
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public int addUser(User user) {
        user.setCreateDate(new Date());
        String s = yyqMD5.yyqMD5(yyqMD5.yyqMD5(user.getPassword()) + user.getName());
        user.setPassword(s);
        userMapper.addUser(user);
        EmailThread emailThread=new EmailThread(user.getEmail(), "飞狐软件","恭喜你是本网站第<font color='red'>"+user.getId()+"</font>个注册成功的!!");
        emailThread.start();
        return user.getId();
    }

    @Override
    public User queryByName(String name) {
        return userMapper.queryByName(name);
    }

    @Override
    public List<Map<String, Object>> queryAllArea() {
        List<Area> areaList = userMapper.queryAllArea();
        return getAreaList(areaList, 0);
    }

    @Override/*   1 登陆成功  2 账号不存在   3 密码错误     */
    public Map login(String name, String password) {
        Map map = new HashMap();
        User user = userMapper.queryByName(name);

        if (user == null) {
            map.put("code", 2);
            return map;
        }

        String s = yyqMD5.yyqMD5(yyqMD5.yyqMD5(password) + name);
        if (!user.getPassword().equals(s)) {
            map.put("code", 3);
            return map;
        }

        map.put("code", 1);
        map.put("data", user);
        return map;
    }

    @Override
    public Integer queryPasswordById(User user) {
        User user1 = userMapper.queryPasswordById(user);
        if (!user1.getPassword().equals(yyqMD5.yyqMD5(yyqMD5.yyqMD5(user.getPassword()) + user.getName()))) {
            return 2;
        }
        return 1;
    }



    @Override
    public void updatePassword(User user) {
        user.setPassword(yyqMD5.yyqMD5(yyqMD5.yyqMD5(user.getPassword()) + user.getName()));
        userMapper.updatePassword(user);
    }

    @Override
        public Object queryUserAllData(UserVo userVo) {
            Map map = new HashMap();
            // 查询总条数
            long count = userMapper.queryCount(userVo);

            //查询分页的数据
            List<User> users = userMapper.queryUserVo(userVo);
            map.put("count", count);
            map.put("msg", "查询成功");
            map.put("data", users);
            map.put("code", 0);
            return map;
        }

    @Override
    public void updateUserById(User user) {
        userMapper.updateUserById(user);
    }

    private List<Map<String, Object>> getAreaList(List<Area> areaList, Integer pid) {
        List<Map<String, Object>> list = new ArrayList<>();
        for (Area area : areaList) {
            Map<String, Object> map = null;
            if (pid.equals(area.getPid())) {
                map = new HashMap<>();
                map.put("id", area.getId());
                map.put("name", area.getName());
                map.put("children", getAreaList(areaList, area.getId()));
                List children = (List) map.get("children");
                if (children.size() == 0) {
                    map.remove("children");
                }
            }
            if (map != null) {
                list.add(map);
            }
        }
        return list;
    }


}
